package com.tjsinfo.tjsoa.useseal.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.jbpm.taskmgmt.exe.TaskInstance;

import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.useseal.bo.ITUseSealSealBO;
import com.tjsinfo.tjsoa.useseal.bo.ITUseSealSealOpeBO;
import com.tjsinfo.tjsoa.useseal.vo.TUseSealSeal;
import com.tjsinfo.tjsoa.useseal.vo.TUseSealSealOpe;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;

public class SealCheckAction extends DispatchAction {
	private ITUseSealSealBO sealBo;
	private ITUseSealSealOpeBO sealOpeBo;
	public void setSealBo(ITUseSealSealBO sealBo) {
		this.sealBo = sealBo;
	}	
	public void setSealOpeBo(ITUseSealSealOpeBO sealOpeBo) {
		this.sealOpeBo = sealOpeBo;
	}	
	
	private void sealOpe(HttpServletRequest request,WorkFlowTemplate test,String sealId,Long taskid,Integer sealMark) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String feedBack = request.getParameter("feedBack");
		Integer checkId = ToolKit.getSessionUser(request).getUser_id();
		String checkName = ToolKit.getSessionUser(request).getUsername();

		TUseSealSealOpe tusso = new TUseSealSealOpe();
		tusso.setSealId(sealId);
		tusso.setDateTime(sdf.parse(sdf.format(date)));
		tusso.setFeedBack(feedBack);
		tusso.setCheckId(checkId.toString());
		tusso.setCheckName(checkName);
		tusso.setTaskid(Integer.parseInt(taskid.toString()));
		TaskInstance taskInstance = test.nextStep(taskid);
		String node = taskInstance.getName();
		tusso.setNode(node);
		tusso.setMark(sealMark);
		sealOpeBo.save(tusso);
	}
	
	//一般公章用印审批

	public ActionForward genCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String sealId = request.getParameter("sealId");
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			Integer userId = ToolKit.getSessionUser(request).getUser_id();
			String userName = ToolKit.getSessionUser(request).getUsername();
			String linkurl = "useSeal/gen/genCheckList.jsp?method=checkList";
			String message = "useSeal.genCheck.message";
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			sealOpe(request,test,sealId,taskid,0);
			
			TaskInstance taskInstance = test.nextStep(taskid);
			String node1 = taskInstance.getName();
			TUseSealSeal tuss = sealBo.findById(sealId);
			tuss.setUserId(tuss.getUserId()+userId+",");
			if("办公室主管审批".equals(node1)){
				String verify = request.getParameter("verify");
				String office1Id = request.getParameter("office1Id");				
				Long flowid = Long.parseLong(tuss.getFlowid().toString());
				if("1".equals(verify)){
					test.setVariable("var1","Y", flowid);
					test.setVariable("办公室主任审批",office1Id, flowid);
				}else{
					test.setVariable("var1","N", flowid);
					tuss.setStatus("完毕");
				}
				tuss.setSendId(userId.toString());
				tuss.setSendName(userName);				
			}else if("办公室主任审批".equals(node1)){
				tuss.setStatus("完毕");
			}else if("申请人修改".equals(node1)){
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date1 = new Date();
				String sealName = request.getParameter("sealName");
				String sealRea = request.getParameter("sealRea");
				String remarks = request.getParameter("remarks");
				
				tuss.setDateTime(sdf1.parse(sdf1.format(date1)));
				tuss.setSealName(sealName);
				tuss.setSealRea(sealRea);
				tuss.setRemarks(remarks);
				tuss.setStatus("审核中");
				linkurl = "useSeal/gen/genList.jsp";
				message = "useSeal.genAdd.message";				
			}
			
			sealBo.update(tuss);
			
			test.nextStep(taskid,null);	
			
			request.setAttribute("linkurl", linkurl);
			request.setAttribute("message", message);	
			
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}
	
	public ActionForward genReturnCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String sealId = request.getParameter("sealId");
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			sealOpe(request,test,sealId,taskid,0);
			
			TUseSealSeal tuss = sealBo.findById(sealId);
			tuss.setUserId(tuss.getUserId()+ToolKit.getSessionUser(request).getUser_id()+",");
			tuss.setStatus("退回");
			sealBo.update(tuss);
			
			Long flowid = Long.parseLong(tuss.getFlowid().toString());
			test.setVariable("申请人修改",tuss.getApplyId(), flowid);
			test.nextStep(taskid,"to 申请人修改");	
			
			request.setAttribute("linkurl", "useSeal/gen/genCheckList.jsp?method=checkList");
			request.setAttribute("message", "useSeal.genCheck.message");	
			
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}	
	
	//重要事项用印审批
	public ActionForward impCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String sealId = request.getParameter("sealId");
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			Integer userId = ToolKit.getSessionUser(request).getUser_id();
			String userName = ToolKit.getSessionUser(request).getUsername();
			String linkurl = "useSeal/imp/impCheckList.jsp?method=checkList";
			String message = "useSeal.impCheck.message";
			String nextStep = null;
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			sealOpe(request,test,sealId,taskid,1);
			
			TaskInstance taskInstance = test.nextStep(taskid);
			String node1 = taskInstance.getName();
			TUseSealSeal tuss = sealBo.findById(sealId);
			tuss.setUserId(tuss.getUserId()+userId+",");
			
			Long flowid = Long.parseLong(tuss.getFlowid().toString());
			if("申请人所在部门主管领导审批".equals(node1)){
				String verify1 = request.getParameter("verify1");
				String verify2 = request.getParameter("verify2");
				String nameId = request.getParameter("name");
				String leaderId = request.getParameter("leaderId");
				nextStep = "to 使用人名章";
				
				if("1".equals(verify1) && !"1".equals(verify2)){
					nextStep = "to 人名章本人审批";
					test.setVariable("人名章本人审批",nameId, flowid);
				}
				
				if(!"1".equals(verify1) && "1".equals(verify2)){
					nextStep = "to 老总审批";
					test.setVariable("老总审批",leaderId, flowid);
				}				
				
				if("1".equals(verify1) && "1".equals(verify2)){
					nextStep = "to 人名章本人审批";
					test.setVariable("人名章本人审批",nameId, flowid);
					test.setVariable("老总审批",leaderId, flowid);
				}
				
				if(!"1".equals(verify1) && !"1".equals(verify2)){
					nextStep = null;
				}				
				
//				if("1".equals(verify1)){
//					test.setVariable("var2","Y", flowid);
//					test.setVariable("人名章本人审批",nameId, flowid);
//					if("1".equals(verify2)){
//						test.setVariable("老总审批",leaderId, flowid);
//					}	
//				}else{
//					test.setVariable("var2","N", flowid);
//					if("1".equals(verify2)){
//						test.setVariable("var3","Y", flowid);
//						test.setVariable("老总审批",leaderId, flowid);
//					}else{
//						test.setVariable("verify2","N", flowid);
//					}		
//				}
			}else if("人名章本人审批".equals(node1)){
				Object leaderId = test.getVariable("老总审批", flowid);
				if(leaderId!=null){
					//test.setVariable("var3","Y", flowid);
					nextStep = "to 老总审批";
				}else{
					//test.setVariable("var3","N", flowid);
					nextStep = null;
				}	
				
			}else if("办公室主管审批".equals(node1)){
				String verify = request.getParameter("verify");
				String office1Id = request.getParameter("office1Id");
				if("1".equals(verify)){
					test.setVariable("var1","Y", flowid);
					test.setVariable("办公室主任审批",office1Id, flowid);
				}else{
					test.setVariable("var1","N", flowid);
					tuss.setStatus("完毕");
				}
				tuss.setSendId(userId.toString());
				tuss.setSendName(userName);					
			}else if("办公室主任审批".equals(node1)){
				tuss.setStatus("完毕");
			}else if("申请人修改".equals(node1)){
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date1 = new Date();
				String sealName = request.getParameter("sealName");
				String sealRea = request.getParameter("sealRea");
				String remarks = request.getParameter("remarks");
				
				tuss.setDateTime(sdf1.parse(sdf1.format(date1)));
				tuss.setSealName(sealName);
				tuss.setSealRea(sealRea);
				tuss.setRemarks(remarks);
				tuss.setStatus("审核中");
				linkurl = "useSeal/imp/impList.jsp";
				message = "useSeal.impAdd.message";				
			}
			
			sealBo.update(tuss);
			
			test.nextStep(taskid,nextStep);	
			
			request.setAttribute("linkurl", linkurl);
			request.setAttribute("message", message);	
			
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}
	
	public ActionForward impReturnCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String sealId = request.getParameter("sealId");
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			sealOpe(request,test,sealId,taskid,1);
			
			TUseSealSeal tuss = sealBo.findById(sealId);
			tuss.setUserId(tuss.getUserId()+ToolKit.getSessionUser(request).getUser_id()+",");
			tuss.setStatus("退回");
			sealBo.update(tuss);
			
			Long flowid = Long.parseLong(tuss.getFlowid().toString());
			test.setVariable("申请人修改",tuss.getApplyId(), flowid);
			test.nextStep(taskid,"to 申请人修改");	
			
			request.setAttribute("linkurl", "useSeal/imp/impCheckList.jsp?method=checkList");
			request.setAttribute("message", "useSeal.impCheck.message");	
			
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}
	
	//人名章用印审批

	public ActionForward nameCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String sealId = request.getParameter("sealId");
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			Integer userId = ToolKit.getSessionUser(request).getUser_id();
			String userName = ToolKit.getSessionUser(request).getUsername();
			String linkurl = "useSeal/name/nameCheckList.jsp?method=checkList";
			String message = "useSeal.nameCheck.message";
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			sealOpe(request,test,sealId,taskid,2);
			
			TaskInstance taskInstance = test.nextStep(taskid);
			String node1 = taskInstance.getName();
			TUseSealSeal tuss = sealBo.findById(sealId);
			tuss.setUserId(tuss.getUserId()+userId+",");
			if("办公室主管审批".equals(node1)){
				String verify = request.getParameter("verify");
				String office1Id = request.getParameter("office1Id");				
				Long flowid = Long.parseLong(tuss.getFlowid().toString());
				if("1".equals(verify)){
					test.setVariable("var1","Y", flowid);
					test.setVariable("办公室主任审批",office1Id, flowid);
				}else{
					test.setVariable("var1","N", flowid);
					tuss.setStatus("完毕");
				}
				tuss.setSendId(userId.toString());
				tuss.setSendName(userName);					
			}else if("办公室主任审批".equals(node1)){
				tuss.setStatus("完毕");
			}else if("申请人修改".equals(node1)){
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date1 = new Date();
				String sealName = request.getParameter("sealName");
				String sealRea = request.getParameter("sealRea");
				String remarks = request.getParameter("remarks");
				
				tuss.setDateTime(sdf1.parse(sdf1.format(date1)));
				tuss.setSealName(sealName);
				tuss.setSealRea(sealRea);
				tuss.setRemarks(remarks);
				tuss.setStatus("审核中");
				linkurl = "useSeal/name/nameList.jsp";
				message = "useSeal.nameAdd.message";				
			}
			
			sealBo.update(tuss);
			
			test.nextStep(taskid,null);	
			
			request.setAttribute("linkurl", linkurl);
			request.setAttribute("message", message);	
			
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}
	
	public ActionForward nameReturnCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String sealId = request.getParameter("sealId");
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			sealOpe(request,test,sealId,taskid,2);
			
			TUseSealSeal tuss = sealBo.findById(sealId);
			tuss.setUserId(tuss.getUserId()+ToolKit.getSessionUser(request).getUser_id()+",");
			tuss.setStatus("退回");
			sealBo.update(tuss);
			
			Long flowid = Long.parseLong(tuss.getFlowid().toString());
			test.setVariable("申请人修改",tuss.getApplyId(), flowid);
			test.nextStep(taskid,"to 申请人修改");	
			
			request.setAttribute("linkurl", "useSeal/name/nameCheckList.jsp?method=checkList");
			request.setAttribute("message", "useSeal.nameCheck.message");	
			
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}		
}
